package dome1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode {
        char value;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char value) {
            this.value = value;
        }
    }

    public TreeNode createBinaryTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    //前序遍历
    public void perOrder(TreeNode root) {
        if (root == null) {
            System.out.print("null ");
            return;
        }
        System.out.printf(root.value + " ");
        perOrder(root.left);
        perOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            System.out.print("null ");
            return;
        }
        inOrder(root.left);
        System.out.print(root.value + " ");
        inOrder(root.right);
    }

    //后序遍历
    public void piostOrder(TreeNode root) {
        if (root == null) {
            System.out.print("null ");
            return;
        }
        inOrder(root.left);
        inOrder(root.right);
        System.out.print(root.value + " ");
    }

    // 获取树中节点的个数
    //1.遍历思路
    public int nodeSize;

    public void getNodeSize(TreeNode root) {
        if (root == null) return;
        nodeSize++;
        getNodeSize(root.left);
        getNodeSize(root.right);
    }

    //2.子问题思路
    // 整颗树的节点个数 =
    //左树的节点个数 + 右树的节点个数 + 1
    public int getNodeSize2(TreeNode root) {
        if (root == null) return 0;
        return getNodeSize2(root.left) +
                getNodeSize2(root.right) + 1;
    }

    // 获取叶子节点的个数
    //1.遍历思路
    public int leafCount;

    public void getLeafNodeCount(TreeNode root) {
        if (root == null) return;
        if (root.left == null && root.right == null) leafCount++;
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }

    //2.子问题思路
    public int getLeafCount2(TreeNode root) {
        if (root == null) return 0;
        if (root.left == null && root.right == null) return 1;
        return getLeafCount2(root.left) + getLeafCount2(root.right);
    }

    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) return 0;
        if (k == 1) return 1;
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }

    // 获取二叉树的高度
    public int getHeight(TreeNode root) {
        if (root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return Math.max(leftHeight, rightHeight) + 1;
    }

    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, char val) {
        if (root == null) return null;
        if (root.value == val) return root;
        TreeNode leftResult = find(root.left, val);
        if (leftResult != null) return leftResult;
        TreeNode rightResult = find(root.right, val);
        if (rightResult != null) return rightResult;
        return null;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if (root == null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.value + " ");
            if (cur.left != null) queue.offer(cur.left);
            if (cur.right != null) queue.offer(cur.right);
        }
    }

    public List<List<Character>> levelOrder2(TreeNode root) {
        if (root == null) return null;
        List<List<Character>> lists = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Character> list = new ArrayList<>();
            int size = queue.size();
            while(size != 0) {
                TreeNode cur = queue.poll();
                size--;
                list.add(cur.value);
                if(cur.left!=null) {
                    queue.offer(cur.left);
                }
                if(cur.right!=null) {
                    queue.offer(cur.right);
                }
            }
            lists.add(list);
        }
        return lists;
    }

    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if (root == null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while(!queue.isEmpty()) {
            TreeNode cur = queue.peek();
            if(cur != null) {
                return false;
            }else {
                queue.poll();
            }
        }
        return true;
    }
}
